/**
 * 不能直接查看的文件
 */
var downloadUtils = require('scripts/Core/DownLoadUtil.js');
var plusStorageManagers = require('script/plusStorageManagers.js');
var localStorageManagers = require('script/localStorageManagers.js');
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack: true
});
//plus初始化
mui.plusReady(function() {
	plusReady();
});
//plus初始化
function plusReady() {

	var self = plus.webview.currentWebview();
	var courseid=self.courseid;
	var catalogid=self.catalogid;
	var catalogname=self.catalogname;
	var rescatname=self.rescatname;
	var resid = self.resid;
	var resname = self.resname;
	var respath = self.respath;
	document.getElementById('fCourseid').value=courseid;
	document.getElementById('fCatalogid').value=catalogid;
	document.getElementById('fCatalogname').value=catalogname;
	document.getElementById('fResid').value=resid;
	document.getElementById('fResname').value=resname;
	document.getElementById('fRescatname').value=rescatname;
	if(resname.length>15)resname=resname.substring(0,15)+'...';
	document.getElementById('resnameh1').innerHTML = resname;
	
	//新增问题
	mui('header').on('tap', '.openputques', openPutques);
	//保存提问
	mui('.mui-content').on('tap', '.submitques', savePutques);
	//取消提问
	mui('.mui-content').on('tap', '.cancelques', cancelQues);
	//打开笔记记录页面
	mui('header').on('tap', '#opennoteview', openNoteview);
	document.getElementById('resnameh1').innerHTML=resname;
				//其他应用打开
			    mui('.footbtndiv').on('tap','.footbtnspan',function(){
			    	respath=downloadUtils.getLocalAbsoluteUrl2(respath);//打开文件需要相对路径
			    	console.log(respath);
			    	var suffix=respath.substring(respath.lastIndexOf('.')+1,respath.length);
			    	if(respath.indexOf('http:')>=0){
			    	 //mui.toast('请先下载到本地');
			    	plus.runtime.openURL(respath, {}, function ( e ) {
			                  plus.nativeUI.alert( "无法打开'"+suffix+"'格式的文件,请安装相关应用." );
		             } );
		            }else{
		             plus.runtime.openFile(respath, {}, function ( e ) {
			                  plus.nativeUI.alert( "无法打开'"+suffix+"'格式的文件,请安装相关应用。" );
		             } );	
		            }
			    });
};

function resView(resid) {
	var item = plusStorageManagers.getTeachPlanSession(resid);
	if (item != null) {
		document.getElementById('rescontent').innerHTML = item;
		return;
	}
	//本地没有存储则ajax
	mui.ajax(serverAjax + 'teacher/teachresource/getResourceById.do', {
		data: {
			resid: resid
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			document.getElementById('rescontent').innerHTML = data.resContent;
			plusStorageManagers.setTeachPlanSession(resid, data.resContent);
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(xhr);
			ajaxErrorTip();
		}
	});
};
//打开新增问题框
function openPutques() {
	document.getElementById('fParentid').value = 0;
	document.getElementById('questextarea').value = '';
	document.getElementById('putques-div').style.display = 'block';
};
//保存问题
function savePutques() {
	if (!hasNet()) return;
	var userid = localStorageManagers.getUseridSession();
	var courseid = document.getElementById('fCourseid').value;
	var catalogid = document.getElementById('fCatalogid').value;
	var resid = document.getElementById('fResid').value;
	var quesContent = document.getElementById('questextarea').value;
	var parentId = document.getElementById('fParentid').value;
	if (quesContent.length == 0) {
		mui.toast("请填写问题内容");
		return;
	}
	if (quesContent.length > 200) {
		mui.toast("最多不超过200字！");
		return;
	}
	var data = {
		userId: userid,
		resId: resid,
		catalogId: catalogid,
		courseId: courseid,
		questionContent: quesContent,
		parentId: parentId
	};
	ajaxSubmitQuestion(data);
	document.getElementById('putques-div').style.display = 'none';
}
//取消问题回复框
function cancelQues() {
	document.getElementById('putques-div').style.display = 'none';
}
//是否有网
function hasNet() {
	var isNet = mobileFrame.IsNetWorkCanUse();
	return isNet;
}
//打开笔记记录页面
function openNoteview() {
	var courseid = document.getElementById('fCourseid').value;
	var catalogid = document.getElementById('fCatalogid').value;
	var resid = document.getElementById('fResid').value;
	var restitle = document.getElementById('fResname').value;
	var rescatname = document.getElementById('fRescatname').value;
	if (resid == 0 || resid == '') {
		mui.toast('请选择资源');
		return;
	}
	mui.openWindow({
		url: 'addNote.html',
		id: 'selfcourseview/addNote',
		styles:getStyles(),
		extras: {
			'courseid': courseid,
			'catalogid': catalogid,
			'resid': resid,
			'restitle': restitle,
			'rescatname': rescatname
		}
	});
};
//ajax保存问题
function ajaxSubmitQuestion(data){
	mui.ajax(serverAjax + '/mobile/question/SaveQuestion.do', {
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(result) {
             if(result!=0){
             	mui.toast('保存成功');
             		var parentWebView = plus.webview.getWebviewById("selfcourseview/resView");
	                parentWebView.evalJS("loadQuestions()");
             	//loadQuestions();
             }else{
             	mui.toast('保存失败');
             }
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr);
		}
	});
};